// pages/home/index.js
import {countOrderByState} from './../../api/order'
const dayjs = require('dayjs')

Page({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    active: 0,
    timeStart: dayjs().format('YYYY-MM-DD 00:00:00'),
    timeEnd: dayjs().add(1, 'day').format('YYYY-MM-DD 00:00:00'),
    statusList: [{
      title: '全部',
      value: '',
      number: 0,
    },{
      title: '待配送',
      value: '6',
      number: 0,
    },{
      title: '中转中',
      value: '12',
      number: 0,
    },{
      title: '配送中',
      value: '7',
      number: 0,
    },{
      title: '已完成',
      value: '8',
      number: 0,
    }]
  },

  onTabChange: function (e) {
    const {index} = e.detail
    const com = this.selectComponent(`#status${index}`)
    com && com.refreshList();
    console.log(com)
    this.data.active = index;
  },
  loadCount: function () {
    const {statusList, timeEnd, timeStart} = this.data
    countOrderByState({
      delyStartTime: timeStart,
      delyEndTimeEnd: timeEnd,
    }).then(res => {
      const map = res.data
      statusList[0].title = '全部 ' + map.all || ''
      statusList[1].title = '待配送 ' + map.state6 || ''
      statusList[2].title = '中转中 ' + map.state12 || ''
      statusList[3].title = '配送中 ' + map.state7 || ''
      statusList[4].title = '已完成 ' + map.state8 || ''
      this.setData({
        statusList
      })
    })
  },
  // 修改日期
  handledDateRange(event) {
    const {start, end} = event.detail
    this.setData({
      timeStart: start,
      timeEnd: end
    })
    console.log(start, end)
  },
  // 加载数据
  loadData() {
    const com = this.selectComponent(`#status${this.data.active}`)
    com && com.refreshList();
    console.log(this.data.active)
  },
  onShow() {
    this.loadCount()
  },
  onLoad() {
    const com = this.selectComponent(`#status0`)
    com && com.refreshList();
    wx.$event.on('e-load-order-count', this, () => {
      this.loadCount()
    })
    const {timeStart, timeEnd} = this.data
    const dateRangeCom = this.selectComponent('#dateRange')
    dateRangeCom.setDefaultDate([timeStart, timeEnd])
  },
  onUnload() {
    wx.$event.remove('e-load-order-count')
  }
})